<template>
  <div id="register">
    <div class="form">
      <div class="title"></div>
      <div class="main">
        <el-form :rules="rules" ref="formRef" :model="formDate">
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" v-model="formDate.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="formDate.password" type="password" showPassword></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input
              placeholder="请再次确认密码"
              v-model="formDate.repassword"
              type="password"
              showPassword
            ></el-input>
          </el-form-item>
          <el-button size="small" type="primary" @click="register">注册</el-button>
          <el-form-item>
            <el-link :underline="false" @click="$router.replace('/login')">去登录</el-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入 registerAPI发起注册请求
import { registerAPI } from '@/api/Register.js'
export default {
  name: 'Register',
  data() {
    // 再次确认密码输入框的自定义验证规则
    const validatePass = (rule, value, callback) => {
      if (value !== this.formDate.password) return callback(new Error('两次输入的密码不一致'))
      callback()
    }
    return {
      // 表单双向绑定数据
      formDate: {
        username: '',
        password: '',
        repassword: ''
      },
      // 表单的校验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[1-9a-zA-Z]{2,10}$/, message: '用户名必须是2-10的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: 'true', message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 注册按钮点击事件
    register() {
      // element-ui的兜底校验
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const {
          data: { code, message }
        } = await registerAPI(this.formDate)
        // 不成功,给提示
        if (code !== 0) return this.$message.error(message)
        // 成功,给提示
        this.$message.success(message)
        // 清除数据
        this.$refs.formRef.resetFields()
        // 跳转至登录页
        this.$router.replace('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
#register {
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;
  .form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    height: 350px;
    background-color: #fff;
    border-radius: 15px;
    padding: 0 30px;
    box-sizing: border-box;
    .title {
      background: url('../../assets/images/login_title.png') no-repeat center;
      height: 60px;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
